home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / telecom / 16 / pascal / istrval.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1986-05-13  |  4.6 KB  |  136 lines

  1. (* Examples of "str" and "val" routines for Personal Pascal. *)
  2.  
  3. PROGRAM str_val;
  4.  
  5.   TYPE
  6.     (* Since we aren't including any of the GEM libraries in this demo, we need
  7.       to declare the STRING type we will work with. *)
  8.     str255 = STRING[ 255 ];
  9.  
  10.   VAR
  11.     i, n: integer;
  12.     s: str255;
  13.  
  14.  
  15.  
  16.   (* str - Convert the integer in the parameter 'n' to a string in 's'.  The
  17.       string may consist of a minus sign ('-'), followed by up to 5 digits of
  18.       the number.  The string will be the minimal length which will hold the
  19.       number (i.e., leading plus signs and leading zeros will NOT appear in
  20.       the final string!). *)
  21.  
  22.   PROCEDURE str( n: integer; VAR s: str255 );
  23.  
  24.     VAR
  25.       digit,            (* Holds each digit value of 'n' as it is created *)
  26.       divisor,          (* Division by this is used to find each digit *)
  27.       i: integer;       (* Index in string at which to put next character *)
  28.       leading: boolean; (* True, if the next digit will be the leading digit *)
  29.  
  30.     (* add_char - Add a single character to the string, incrementing the current
  31.         index. *)
  32.  
  33.     PROCEDURE add_char( c: char );
  34.  
  35.       BEGIN
  36.         i := i + 1;
  37.         s[i] := c;
  38.       END;
  39.  
  40.     BEGIN (* str - main routine *)
  41.       i := 0;           (* Start at the beginning of the string *)
  42.       IF n < 0 THEN     (* If the number is negative, add a minus sign *)
  43.         BEGIN
  44.           add_char( '-' );
  45.           n := -n;
  46.         END;
  47.       (* Now divide the number by decreasing divisors to form each digit-- the
  48.         divisor starts at 10000, since this is the maximum power of 10 which
  49.         will fit into a positive integer. *)
  50.       divisor := 10000;
  51.       leading := true;
  52.       WHILE divisor > 0 DO
  53.         BEGIN
  54.           (* Get the next digit value.  If the digit is not zero, or the digit
  55.             will not be the leading digit, then add it to the string (this
  56.             inhibits the addition of leading zeros). *)
  57.           digit := n DIV divisor;
  58.           IF (digit <> 0) OR NOT( leading ) THEN
  59.             BEGIN
  60.               add_char( chr(digit + ord('0')) );
  61.               leading := false;
  62.             END;
  63.           (* Throw away the part of the number just used, and decrease the
  64.             divisor so we will get the next digit next time. *)
  65.           n := n MOD divisor;
  66.           divisor := divisor DIV 10;
  67.         END;
  68.       (* At this point, if the index is still zero, then we didn't add any
  69.         characters to the string!  The original number must have been zero, so
  70.         just add that single character. *)
  71.       IF i = 0 THEN
  72.         add_char( '0' );
  73.       (* Finally, set the length of the string to the final index value. *)
  74.       s[0] := chr(i);
  75.     END;
  76.  
  77.  
  78.   (* val - Convert the number contained in the string 's' to an integer, and
  79.       return that integer as the function result.  We are assuming the caller
  80.       has ensured the string is a valid number, so we we're just going to
  81.       convert characters into the number until a non-digit is encountered, or
  82.       the end of the string is reached.  A zero is returned as the function
  83.       value if the string that was passed has zero length. *)
  84.  
  85.   FUNCTION val( s: str255 ): integer;
  86.  
  87.     VAR
  88.       (* Flag to indicate the number has a leading minus sign *)
  89.       minus: boolean;
  90.  
  91.     BEGIN
  92.       (* Start with the first character of the string, but first skip leading
  93.         blanks. *)
  94.       i := 1;
  95.       WHILE (i < length(s)) AND (s[i] = ' ') DO
  96.         i := i + 1;
  97.       (* If there are characters still in the string, convert it to a number *)
  98.       n := 0;
  99.       IF length(s) >= i THEN
  100.         BEGIN
  101.           (* If first char is '-', we have to negate the number after we finish
  102.             converting the digits. *)
  103.           IF s[i] <> '-' THEN
  104.             minus := false
  105.           ELSE
  106.             BEGIN
  107.               minus := true;
  108.               i := i + 1;
  109.             END;
  110.           (* While there are more digits in the string, convert characters. *)
  111.           WHILE (i <= length(s)) AND (s[i] IN ['0'..'9']) DO
  112.             BEGIN
  113.               n := (n * 10) + ord(s[i]) - ord('0');
  114.               i := i + 1;
  115.             END;
  116.           (* Negate the final result, if necessary. *)
  117.           IF minus THEN
  118.             n := -n;
  119.         END;
  120.       (* Return the converted number as the result of this function. *)
  121.       val := n;
  122.     END;
  123.  
  124.  
  125.  
  126.   BEGIN
  127.     FOR i := -10 TO 10 DO
  128.       BEGIN
  129.         readln( s );
  130.         n := val(s);
  131.         write( n, ' = ' );
  132.         str( n, s );
  133.         writeln( s );
  134.       END;
  135.   END.
  136. GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG